package com.gc.ip.demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.gc.ip.IPAddress;
import com.gc.ip.JMultiIPAddress;
import com.gc.ip.JSingleIPAddress;
import com.gc.ip.util.CheckIPUtils;

@SuppressWarnings("serial")
public class IPParamPanel extends JPanel
{
	private JPanel multiPanel = null;
	private JPanel singlePanel = null;
	private JPanel buttonPanel = null;
	
	private JCheckBox multiCheckBox = null;
	private JCheckBox singleCheckBox = null;
	
	private JMultiIPAddress multiIPAddress = null;
	private JSingleIPAddress singleIPAddress = null;
	
	private JButton updateButton = null;
	private JButton refreshButton = null;
	private JButton exitButton = null;
	
	public IPParamPanel()
	{
		initGUI();
		addListeners();
	}
	
	private void initGUI()
	{
		initMultiPanel();
		initSinglePanel();
		initButtonPanel();
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(multiPanel, BorderLayout.CENTER);
		mainPanel.add(singlePanel, BorderLayout.SOUTH);
		
		this.setLayout(new BorderLayout());
		this.add(mainPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	private void initMultiPanel()
	{
		multiPanel = new JPanel();
		multiPanel.setLayout(new GridBagLayout());
		
		multiPanel.add(new JLabel("Name:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		multiPanel.add(new JTextField(), new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		multiCheckBox = new JCheckBox("Enable Multiple IP Address", true); 
		multiPanel.add(multiCheckBox, new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		multiIPAddress = new JMultiIPAddress(); 
		multiPanel.add(multiIPAddress, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0, GridBagConstraints.WEST,
			GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		
		multiPanel.setBorder(new TitledBorder("Multiple IP Address Parameters"));
	}
	
	private void initSinglePanel()
	{
		singlePanel = new JPanel();
		singlePanel.setLayout(new GridBagLayout()); 
		
		singlePanel.add(new JLabel("Description:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,
			GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		singlePanel.add(new JTextField(), new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		singleCheckBox = new JCheckBox("Enable Single IP Address", true);
		singlePanel.add(singleCheckBox, new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		singleIPAddress = new JSingleIPAddress(); 
		singlePanel.add(singleIPAddress, new GridBagConstraints(0, 2, 2, 1, 1.0, 0.0, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		singlePanel.setBorder(new TitledBorder("Single IP Address Parameters"));
	}
	
	private void initButtonPanel()
	{
		updateButton = new JButton("Update");
		refreshButton = new JButton("Refresh");
		exitButton = new JButton("Exit");
		
		buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(updateButton);
		buttonPanel.add(refreshButton);
		buttonPanel.add(exitButton);
	}
	
	private void addListeners()
	{
		multiCheckBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				multiIPAddress.setEnabled(multiCheckBox.isSelected());
			}
		});
		singleCheckBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				singleIPAddress.setEnabled(singleCheckBox.isSelected());
			}
		});
		
		updateButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				update();
			}
		});
		
		exitButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	private void update()
	{
		//TODO
		List<IPAddress> ipAddressList = multiIPAddress.getAllIPAddress();
		
		List<IPAddress> dupIPs = CheckIPUtils.checkDuplication(ipAddressList);
		if(dupIPs != null && !dupIPs.isEmpty())
		{
			int option = JOptionPane.showConfirmDialog(this.getParent(), buildWarningMessage(dupIPs), "Warning",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if(option != JOptionPane.YES_OPTION)
			{
				return;
			}
			
			//go on
		}
	}
	
	private String buildWarningMessage(List<IPAddress> dupIPs)
	{
		return "The IP Address is already used by ... Do you still want to do the configuration?";
	}
	
	public static void main(String[] args) throws Exception
	{
//		javax.swing.UIManager.put("swing.boldMetal", Boolean.FALSE);
//		javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		JFrame frame = new JFrame("JIPAddress Demo");

		IPParamPanel ipPanel = new IPParamPanel();
		frame.getContentPane().add(ipPanel);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800, 600));
		frame.pack();
		frame.setVisible(true);
	}
}
